package com.sbr.seb.wicket.page;

import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.util.time.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sbr.seb.entity.Log;
import com.sbr.seb.wicket.entity.LogLabel;

/**
 * Homepage
 */
public class LogPage extends BasePage {

	private static final long serialVersionUID = 1L;

	/**
	 * Constructor that is invoked when page is invoked without a session.
	 * 
	 * @param parameters
	 *            Page parameters
	 */	
	public LogPage() {
		super();
		final List<Log> logList = new ArrayList<Log>();
		logList.add(new Log("22/01/2010 | INFO | Server | NullPointerEx..."));
		logList.add(new Log("22/01/2010 | ERROR | LOG | NullPointerEx..."));
		final List<Log> logList2 = new ArrayList<Log>(logList);
		

		Thread t = new Thread(new Runnable() {

			private final Logger logger = LoggerFactory.getLogger(LogPage.class);


			@Override
			public void run() {
				int i=0;
				logger.debug("Thread start");
				while(i<10) {
					logList.add(new Log("22/01/2010 "+i+" | INFO | Server | NullPointerEx...\n"));
					logger.debug("thread logList size = {}", logList.size());
					i++;
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						logger.debug("ERROR", e);
					}
				}
				logger.debug("Thread stop");
			}
		});
		t.start();

		
		add(new ListView<Log>("logList", logList2) {

			@Override
			protected void populateItem(ListItem<Log> item) {
				Log server = item.getModelObject();
				item.add(new Label("logDate", server.getTimestamp()));
				item.add(new Label("logLevel", server.getLevel()));
				item.add(new Label("logClass", server.getFromClass()));
				item.add(new Label("logMessage", server.getMessage()));

			}

		});

		Clock clock = new Clock("clock", TimeZone.getTimeZone("America/Los_Angeles"));
		add(clock);

		// add the ajax behavior which will keep updating the component every 5
		// seconds
		clock.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(1000000)));

		LogLabel log = new LogLabel("log", logList);
		add(log);

		// add the ajax behavior which will keep updating the component every 5
		// seconds
		log.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(1)));

	}

}
